#include "ProjectileManager.h"
#include "EntityProjectile.h"
#include "WorldModel.h"
#include <assert.h>

CProjectileManager* CProjectileManager::instance=NULL;

CProjectileManager::CProjectileManager(void): indexFirstProjectile(-1), indexLastProjectile(-1)
{}

CProjectileManager& CProjectileManager::getInstance(void)
{
	if(instance==NULL)
	{
		instance=new CProjectileManager();
	}
	return *instance;
}

CProjectileManager::~CProjectileManager(void)
{
	//destruction of the projectiles model
	for (vector<CEntityProjectile*>::iterator p=vectorProjectileTemplate.begin(); p<vectorProjectileTemplate.end(); p++)
	{
		delete (*p);
		(*p)=NULL;
	}	
}

/*function that delete and close the projectile manager*/
void CProjectileManager::Close(void)
{
	delete CProjectileManager::instance;
	CProjectileManager::instance=NULL;
}

/*create a template of projectile*/
void CProjectileManager::createProjectileTemplate(int indexSprite, int indexFrame, int health,  
		 CVector2D speed, int collisionCost, int side, int offsetX, int offsetY)
{
	assert(indexSprite>=0);
	assert(indexFrame>=0);
	assert(health>=0);
	assert(side>=0);

	CEntityProjectile* newProjectile = new CEntityProjectile(indexSprite,indexFrame,speed,0,0,health,
		collisionCost,true,side,offsetX, offsetY);
	this->vectorProjectileTemplate.push_back(newProjectile);

}

/*create all the projectile in the entity vector of the world model.All the projectile are inactive*/
void CProjectileManager::createAllProjectile(int numberOfProjectile)
{
	assert(numberOfProjectile>0);

	this->indexFirstProjectile=WMODEL.getEntityVectorSize();
	for(int i=0;i<numberOfProjectile;i++)
	{
		CWorldEntity* newEntity= new CEntityProjectile(0,1,CVector2D(0,0),0,0,0,0,false,0,0,0);
		WMODEL.addEntity(newEntity);
	}
	this->indexLastProjectile=this->indexFirstProjectile+numberOfProjectile-1;
}

/*spawn a projectile if ther is a free space*/
void CProjectileManager::spawnProjectile(CEntityProjectile* projectileTemplate, int positionX, int positionY)
{
	//look for a free space
	bool found=false;
	int indexProjectile=this->indexFirstProjectile;
	while(indexProjectile<=this->indexLastProjectile && found==false)
	{
		if(WMODEL.getActive(indexProjectile))
		{
			indexProjectile++;
		}
		else
		{
			found=true;
		}
	}
	//if there is a free space we spawn the projectile
	if(found==true)
	{		
		CEntityProjectile* projectile=(CEntityProjectile*)WMODEL.getEntityPnter(indexProjectile);
		(*projectile)=(*projectileTemplate);
		projectile->setCurrentPosX(positionX);
		projectile->setCurrentPosY(positionY);
		CRectangle collisionRectangle(projectileTemplate->getOffsetSpawnX(),
			VIS.getFrameWidth(projectileTemplate->getIndexSprite())+projectileTemplate->getOffsetSpawnX(),
			projectileTemplate->getOffsetSpawnY(),
			VIS.getFrameHeight(projectileTemplate->getIndexSprite())+projectileTemplate->getOffsetSpawnY());
		projectile->setCollisionRectangle(collisionRectangle);
	}

}

/*function that return a pointer to a projectile template*/
CEntityProjectile* CProjectileManager::getProjectileTemplate(int index)
{
	return this->vectorProjectileTemplate[index];
}

int CProjectileManager::getIndexFirstProjectile(void)const
{
	return this->indexFirstProjectile;
}
int CProjectileManager::getIndexLastProjectile(void)const
{
	return this->indexLastProjectile;
}

void CProjectileManager::setIndexFirstProjectile(int indexFirstProjectile)
{
	this->indexFirstProjectile=indexFirstProjectile;
}
void CProjectileManager::setIndexLastProjectile(int indexLastProjectile)
{
	this->indexLastProjectile=indexLastProjectile;
}